﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace SurvivalStrategy.Console
{

    public class ConsoleBar
    {
        private Vector2 position;
        private Texture2D texture;
        private KeyboardState lastKeyboadState;
        private List<string> text;
        private bool isVisable;
        private string currentString;
        List<Command> commands;
        int width;

        public ConsoleBar(Game game, Texture2D texture, int width)
        {
            text = new List<string>();
            commands = new List<Command>();
            this.texture = texture;
            this.position = new Vector2(0, -240);
            AddCommand("Kurczak", Func);
            currentString = "";
            this.width = width;
        }

        void Func(List<string> li)
        {
            AddText("Uwaga! Ktos wlasnie napisal slowo kurczak!");
        }

        public void Update(GameTime gameTime, KeyboardState keyboardState)
        {
            if (lastKeyboadState != keyboardState)
            {
                if (keyboardState.IsKeyDown(Keys.Tab))
                    if (isVisable) Hide();
                    else Show();
                if (isVisable&&currentString.Length < 35)
                {
                    bool shift = false;
                    if (keyboardState.IsKeyDown(Keys.LeftShift) || (keyboardState.IsKeyDown(Keys.RightShift))) shift = true;
                    if (keyboardState.IsKeyDown(Keys.Q) && lastKeyboadState.IsKeyUp(Keys.Q))
                        if (shift) currentString += "Q";
                        else currentString += "q";
                    else if (keyboardState.IsKeyDown(Keys.W) && lastKeyboadState.IsKeyUp(Keys.W))
                        if (shift) currentString += "W";
                        else currentString += "w";
                    else if (keyboardState.IsKeyDown(Keys.E) && lastKeyboadState.IsKeyUp(Keys.E))
                        if (shift) currentString += "E";
                        else currentString += "e";
                    else if (keyboardState.IsKeyDown(Keys.R) && lastKeyboadState.IsKeyUp(Keys.R))
                        if (shift) currentString += "R";
                        else currentString += "r";
                    else if (keyboardState.IsKeyDown(Keys.T) && lastKeyboadState.IsKeyUp(Keys.T))
                        if (shift) currentString += "T";
                        else currentString += "t";
                    else if (keyboardState.IsKeyDown(Keys.Y) && lastKeyboadState.IsKeyUp(Keys.Y))
                        if (shift) currentString += "Y";
                        else currentString += "y";
                    else if (keyboardState.IsKeyDown(Keys.U) && lastKeyboadState.IsKeyUp(Keys.U))
                        if (shift) currentString += "U";
                        else currentString += "u";
                    else if (keyboardState.IsKeyDown(Keys.I) && lastKeyboadState.IsKeyUp(Keys.I))
                        if (shift) currentString += "I";
                        else currentString += "i";
                    else if (keyboardState.IsKeyDown(Keys.O) && lastKeyboadState.IsKeyUp(Keys.O))
                        if (shift) currentString += "O";
                        else currentString += "o";
                    else if (keyboardState.IsKeyDown(Keys.P) && lastKeyboadState.IsKeyUp(Keys.P))
                        if (shift) currentString += "P";
                        else currentString += "P";
                    else if (keyboardState.IsKeyDown(Keys.A) && lastKeyboadState.IsKeyUp(Keys.A))
                        if (shift) currentString += "A";
                        else currentString += "a";
                    else if (keyboardState.IsKeyDown(Keys.S) && lastKeyboadState.IsKeyUp(Keys.S))
                        if (shift) currentString += "S";
                        else currentString += "s";
                    else if (keyboardState.IsKeyDown(Keys.D) && lastKeyboadState.IsKeyUp(Keys.D))
                        if (shift) currentString += "D";
                        else currentString += "d";
                    else if (keyboardState.IsKeyDown(Keys.F) && lastKeyboadState.IsKeyUp(Keys.F))
                        if (shift) currentString += "F";
                        else currentString += "f";
                    else if (keyboardState.IsKeyDown(Keys.G) && lastKeyboadState.IsKeyUp(Keys.G))
                        if (shift) currentString += "G";
                        else currentString += "g";
                    else if (keyboardState.IsKeyDown(Keys.H) && lastKeyboadState.IsKeyUp(Keys.H))
                        if (shift) currentString += "H";
                        else currentString += "h";
                    else if (keyboardState.IsKeyDown(Keys.J) && lastKeyboadState.IsKeyUp(Keys.J))
                        if (shift) currentString += "J";
                        else currentString += "j";
                    else if (keyboardState.IsKeyDown(Keys.K) && lastKeyboadState.IsKeyUp(Keys.K))
                        if (shift) currentString += "K";
                        else currentString += "k";
                    else if (keyboardState.IsKeyDown(Keys.L) && lastKeyboadState.IsKeyUp(Keys.L))
                        if (shift) currentString += "L";
                        else currentString += "l";
                    else if (keyboardState.IsKeyDown(Keys.Z) && lastKeyboadState.IsKeyUp(Keys.Z))
                        if (shift) currentString += "Z";
                        else currentString += "z";
                    else if (keyboardState.IsKeyDown(Keys.X) && lastKeyboadState.IsKeyUp(Keys.X))
                        if (shift) currentString += "X";
                        else currentString += "x";
                    else if (keyboardState.IsKeyDown(Keys.C) && lastKeyboadState.IsKeyUp(Keys.C))
                        if (shift) currentString += "C";
                        else currentString += "c";
                    else if (keyboardState.IsKeyDown(Keys.V) && lastKeyboadState.IsKeyUp(Keys.V))
                        if (shift) currentString += "V";
                        else currentString += "v";
                    else if (keyboardState.IsKeyDown(Keys.B) && lastKeyboadState.IsKeyUp(Keys.B))
                        if (shift) currentString += "B";
                        else currentString += "b";
                    else if (keyboardState.IsKeyDown(Keys.N) && lastKeyboadState.IsKeyUp(Keys.N))
                        if (shift) currentString += "N";
                        else currentString += "n";
                    else if (keyboardState.IsKeyDown(Keys.M) && lastKeyboadState.IsKeyUp(Keys.M))
                        if (shift) currentString += "M";
                        else currentString += "m";
                                        
                    if (keyboardState.IsKeyDown(Keys.Space) && lastKeyboadState.IsKeyUp(Keys.Space))
                        currentString += " ";

                    if (keyboardState.IsKeyDown(Keys.Enter))
                        if(currentString!="")
                        {
                            AddText(currentString);

                            ParseToCommand(currentString);
                            currentString = "";
                        }

                    if (keyboardState.IsKeyDown(Keys.Back))
                        if(currentString.Length>0)
                            currentString =currentString.Substring(0, currentString.Length - 1);
                }
            }

            lastKeyboadState = keyboardState;
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch, SpriteFont spriteFont)
        {
            if (isVisable)
            {
                spriteBatch.Draw(texture, new Rectangle((int)position.X, (int)position.Y, width, 300), Color.White);
                for (int i = 0; i < text.Count; ++i)
                { 
                    int currentY = (int)position.Y + 300 - (i+2)*30;
                    spriteBatch.DrawString(spriteFont, text[text.Count-1 - i], new Vector2(10, currentY), Color.Black);
                }
                spriteBatch.DrawString(spriteFont, currentString+"|", new Vector2(10, 30), Color.Black);
            }
        }

        public void Show()
        {
            isVisable = true;
        }

        public void Hide()
        {
            isVisable = false;
        }

        public void AddText(string s)
        {
            text.Add(s);
        }

        private void CommandHelp()
        {
            int count = 0;
            string str = "Help>";
            string lastCommand = "";
            if (currentString.StartsWith("/"))
            {
                foreach (Command com in commands)
                    if (com.CommandText.StartsWith(currentString))
                    {
                        lastCommand = com.CommandText;
                        str += com.CommandText.Replace('/', ' ');
                        count++;
                    }
                AddText(str);
                if (count == 1)
                    currentString = lastCommand;
            }
        }

        public void ParseToCommand(string s)
        {
            List<string> args = new List<string>();
            
            string newStr = "";
            s += " ";
            for (int i = 0; i < s.Length; ++i)
            {
                if (s[i] == ' ')
                    if (newStr != "")
                    {
                        args.Add(newStr);
                        newStr = "";
                    }
                newStr += s[i];
            }
            foreach (Command command in commands)
            {
                if (args[0] == command.CommandText)
                    command.DoFunction(args);
            }
        }

        public void AddCommand(string commandText, CommandDelegate commandFunction)
        { 
            commands.Add(new Command(commandText, commandFunction));
        }
    }
}
